package com.platinadesign.axa.manager
{
	import com.platinadesign.axa.BaseManager;
	import com.platinadesign.axa.event.PageTurningEvent;
	import com.platinadesign.axa.view.Page;
	import com.platinadesign.axa.view.TurningPages;
	import flash.events.Event;

	public class PagesManager extends BaseManager
	{
		private var _currentPage : Page;

		private var _targetPage : Page;

		private var _pages : Vector.<Page>;

		public function PagesManager()
		{
			this.addViewListener( PageTurningEvent.TURN_PAGE, pageTurningHandler );
			this.addViewListener( TurningPages.ALL_PAGES_TURNED, allPagesTurnedHandler );
		}

		public function initPages( pages : Vector.<Page> ) : void
		{
			_pages = pages;
			_currentPage = _pages[ 0 ];
		}

		public function turnToPage( targetPageIndex : int ) : void
		{
			const targetIndex : int = validatePageIndex( targetPageIndex );
			_targetPage = _pages[ targetIndex ];
			
			//创建翻页效果实例 并 发送出去，交由其他控制类处理
			var overPagesWithOrder : Vector.<Page>;
			var direction : String;


			if( targetIndex == _currentPage.index )
			{
				return;
			}
			else if( targetIndex < _currentPage.index )
			{
				_targetPage.resetContent();
				_currentPage.resetContent();
				overPagesWithOrder = _pages.slice( targetIndex, _currentPage.index + 1 ).reverse();
				direction = PageTurningEvent.TURN_TO_RIGHT;
			}
			else
			{
				_targetPage.resetContent();
				_currentPage.resetContent();
				overPagesWithOrder = _pages.slice( _currentPage.index, targetIndex + 1 );
				direction = PageTurningEvent.TURN_TO_LEFT;
			}
			// TurningPage 类支持翻多页，所以需要所有经过的PAGE的实例，用来制作BITMAP对象
			this.dispatch( new PageTurningEvent( PageTurningEvent.PAGE_TURNING, direction, targetIndex, overPagesWithOrder ));
		}

		private function pageTurningHandler( event : Event ) : void
		{
			//点击角按钮，则按钮不知道当前页是几，所以只派发方向，则目标索引为默认值 -1，这时就按1 进行加减，对吧。
			trace( "Page Turn: " + event[ 'targetIndex' ]);

			if( event[ 'targetIndex' ] == -1 )
			{
				var index : uint;

				if( event[ 'direction' ] == PageTurningEvent.TURN_TO_LEFT )
				{
					index = validatePageIndex( _currentPage.index + 1 );
				}
				else if( event[ 'direction' ] == PageTurningEvent.TURN_TO_RIGHT )
				{
					index = validatePageIndex( _currentPage.index - 1 );
				}
				else
				{
					throw new Error( "wrong direction input" )
				}
				turnToPage( index );
				return;
			}
			//否则，就可以直接按照目标页索引进行翻页
			turnToPage( event[ 'targetIndex' ]); //TODO：控制栏的输入也可能带多页翻页，所以要加入目标页，作为可选参数，要么方向，要么目标页。
		}

		private function validatePageIndex( index : int ) : uint
		{
			//防止越界
			trace( "gotoPage:" + index );
			var targetPageIndex : int = index;

			if( targetPageIndex > _pages.length - 1 )
			{
				targetPageIndex = _pages.length - 1;
			}
			else if( targetPageIndex < 0 )
			{
				targetPageIndex = 0;
			}
			return targetPageIndex;
		}

		private function allPagesTurnedHandler( event : Event ) : void
		{
			_currentPage = _targetPage;
		}
	}
}